package com.weixin.xplore.dao.entity;

import java.util.Date;
import java.util.Map;

/**
 * C端用户实体
 *
 * @author xuyuxiang
 * @date 2022/4/21 16:13
 **/

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.weixin.xplore.dao.common.CommonEntity;
import com.weixin.xplore.utils.JsonUtil;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Lob;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity(name = "client_user")
public class SysUser extends CommonEntity {
    @Lob
    @Column(length = 65_535)
    private String avatar;

    /**
     * 签名
     */
    private String signature;

    /**
     * 账号
     */
    private String account;

    /**
     * 密码
     */
    @JsonIgnore
    private String password;

    /**
     * 姓名
     */
    private String name;

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 性别
     */
    private String gender;

    /**
     * 年龄
     */
    private String age;

    /**
     * 出生日期
     */
    private String birthday;

    /**
     * 通信地址
     */
    private String mailingAddress;

    /**
     * 手机
     */
    private String phone;

    /**
     * 邮箱
     */
    private String email;


    /**
     * 用户状态
     */
    private String userStatus;

    public Boolean getIsVip() {
        String extend = getExtend();
        if(StrUtil.isBlank(extend)){
            return Boolean.FALSE;
        }
        Map<String, Object> map = JsonUtil.toMap(extend, String.class, Object.class);

        return MapUtil.getBool(map,"isVIP",false);
    }

    public Boolean isContinueSign(Long continueDay) {
        String extend = getExtend();
        if(StrUtil.isBlank(extend)){
            return Boolean.FALSE;
        }
        Map<String, Object> map = JsonUtil.toMap(extend, String.class, Object.class);
        Date lastSignDate = MapUtil.getDate(map, "lastSignDate");
        Long continueSignDay = MapUtil.getLong(map, "continueSignDay");
        return continueSignDay==continueDay;
    }

    public Long getCurrentPoints() {
        String extend = getExtend();
        if(StrUtil.isBlank(extend)){
            return 0L;
        }
        Map<String, Object> map = JsonUtil.toMap(extend, String.class, Object.class);

        return MapUtil.getLong(map,"currentPoints",0L);
    }

    public String getInvitationCode() {
        String extend = getExtend();
        if(StrUtil.isBlank(extend)){
            return null;
        }
        Map<String, Object> map = JsonUtil.toMap(extend, String.class, Object.class);

        return MapUtil.getStr(map,"invitationCode",null);
    }
}
